<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;

use App\Models\Apply;
use App\Models\Team;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class TeamController extends Controller
{
    public function apply(Request $request)
    {
        $user = Auth::user();

        if ($user->apply instanceof Apply) {
            return response()->json(['code' => 1001, 'message' => '用户已经申请，请等待审核']);
        }

        $team = Team::query()->where('uuid', $request->input('team_uuid'))->first();

        if ($team instanceof Team) {
            Apply::create([
                'team_id' => $team->id,
                'user_id' => $user->id,
            ]);
            return response()->json(['code' => 1, 'message' => '申请成功，请等待审核']);
        } else {
            return response()->json(['code' => 1002, 'message' => '公司代码不正确'], 400);
        }
    }
}
